package com.geek.config.servlet;

import com.geek.config.microprofile.source.ServletRequestConfigSource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@WebServlet(value = "/config")
public class ConfigServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final ServletRequestConfigSource configSource = new ServletRequestConfigSource(req);
    final PrintWriter writer = resp.getWriter();

    Map<String, String> map = new HashMap<>();
    final Set<String> names = configSource.getPropertyNames();
    for (String name : names) {
      map.put(name, configSource.getValue(name));
    }
    writer.write("config ... " + map.toString());
  }
}
